<?php
/**
 * Created by PhpStorm.
 * User: Cao
 * Date: 09/10/2014
 * Time: 14:24
 */
class checklogin extends CI_Controller
{
    function __construct(){
        parent::__construct();
        $this->load->helper("url");
    }
    public function index(){
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
        if($this->form_validation->run() == FALSE)
        {
            redirect('admincp/FistController', 'refresh');
        }
        else
        {
            $this->load->model("user_model");
            $username = $this->input->post('username');
            $password=$this->input->post('password');
            $result = $this->user_model->login($username,$password);
            if ($result){
                $sess_id =array();
                foreach ($result as $row){
                    $sess_id = array(
                        'id' => $row->user_id,
                        'username' => $row->user_name,
                        'level' =>$row->user_level
                    );
                    $this->session->set_userdata('user_id', $sess_id);
                }
                redirect('admincp/dashboard');
            }
            else{
                $this->form_validation->set_message('checklogin', 'Không đúng tên đăng nh?p và m?t kh?u');
                $this->load->view("login/index");

            }

        }
    }
}